package BLL;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * 检查数据表
 * @ClassName establishTables
 * @Description: TODO
 * @Author YELIUHUISHI
 * @Date 2020/2/29
 * @Version V1.0
 **/


public class establishTables {
    private Statement statement=null;
    public establishTables() {

        String path="src/configuration/sequential_query.properties";
        try {
            FileInputStream fileInputStream = new FileInputStream(path);
            Properties properties = new Properties();
            properties.load(fileInputStream);
            Connection connection = DriverManager.getConnection(properties.getProperty("mysqlLogin"), properties.getProperty("mysqlUser"), properties.getProperty("mysqlPwd"));
            statement=connection.createStatement();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //检查login
    public void  inspectLogin(){
        boolean flag=false;
        String chooseLogin="show tables like 'login'";//判断数据表login是否存在
        String createLogin="CREATE TABLE `login` (\n" +
                "  `name` varchar(50) NOT NULL DEFAULT '',\n" +
                "  `password` varchar(50) DEFAULT NULL,\n" +
                "  PRIMARY KEY (`name`)\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8";//创建数据表login
        try {
            ResultSet resultSet = statement.executeQuery(chooseLogin);
            if (resultSet.next()){
                flag=true;
                statement.close();
            }else{
                statement.executeUpdate(createLogin);
                inspectLogin();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //检查student
    public void inspectStudent(){
        boolean flag=false;
        String chooseStudent="show tables like 'student'";//判断数据表login是否存在
        String createStudent="CREATE TABLE `student` (\n" +
                "  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,\n" +
                "  `name` varchar(50) DEFAULT NULL,\n" +
                "  `sex` varchar(20) DEFAULT NULL,\n" +
                "  `age` int(11) DEFAULT NULL,\n" +
                "  PRIMARY KEY (`id`)\n" +
                ") ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8";//创建数据表login
        try {
            ResultSet resultSet = statement.executeQuery(chooseStudent);
            if (resultSet.next()){
                flag=true;
                statement.close();
            }else{
                statement.executeUpdate(createStudent);
                inspectLogin();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
